import Vue from 'vue'
import co from 'co'
// import CommonService from './CommonService'
import * as Util from '../../components/Util'

// 掉气补气
let offGasAddGasGen = function * (model, row) {
  try {
    // 存库
    let result = yield co(offGasGen(model, row))

    // 获取气价和售气情况参数
    let cardParams = yield co(getParamGen(row.f_card_id, row))

    // 写卡
    let params = {
      factory: row.f_alias,                                // 气表厂家
      kh: row.f_card_id,                                   // 卡号
      kmm: row.f_card_password,                            // 卡密码，写卡后返回新密码, 不论补卡还是先开户都没有密码
      bjql: row.f_police_gas,                              // 报警气量,t_gasbrand表中
      czsx: row.f_topup_ceil,                              // 充值上线，t_gasbrand表中
      tzed: row.f_overdr_lines,                            // 透支额度，t_gasbrand表中
      bkcs: model.f_replace_time,                          // 补卡次数，从补卡表里通过表编号获得
      ql: model.f_fillgas,                                 // 预购气量
      cs: row.f_times,                                     // 求购气次数，先开户为1
      money: model.f_fillamount                            // 购气金额
    }
    params = Object.assign({}, Vue.CommonService.writeCardParams(cardParams), params)
    let cardRes = yield co(Vue.resetpost('http://127.0.0.1:8003/WriteGasCard', params, {resolveMsg: null, rejectMsg: '补气卡服务错误'}))

    // 卡服务写卡异常，业务逻辑回滚
    if (!cardParams || !result || cardRes.data.Err || cardRes.data.Exception) {
      let data = {
        userfile: {
          f_user_id: row.f_user_id,
          f_remanent_gas: model.f_lastremain_fillgas,
          version: 0
        },
        fillgas_id: result.data.id
      }
      yield Vue.resetpost('rs/logic/addGasRoolBack', {data: {userfile: data.userfile, fillgas_id: data.fillgas_id}}, {resolveMsg: '补气失败', rejectMsg: '补气失败'})
    } else {
      let update = {
        userfiles: {},
        record: {
          id: result.data.id,
          f_state: '有效'
        }
      }
      yield Vue.resetpost('rs/logic/updatemark', {data: update}, {resolveMsg: '补气成功', rejectMsg: '补气修改状态失败'})
    }
  } catch (error) {
    Vue.console.error('出错', error)
  }
}

let offGasGen = function * (model, row) {
  let data = {
    f_card_id: row.f_card_id,
    f_fillgas: model.f_fillgas,
    f_fillgas_fee: row.f_total_fee,
    f_lastremain_fillgas: model.f_lastremain_fillgas,
    f_theremain_fillgas: model.f_theremain_fillgas,
    f_reason: model.f_reason[0],
    f_othereason: model.f_othereason,
    f_fillgas_type: '掉气补气',
    f_fillgas_state: '有效',
    f_user_id: row.f_user_id,
    record_userinfo: row.f_userinfo_id,
    f_userinfo_id: row.f_userinfo_id,
    f_user_name: row.f_user_name,
    f_operat_type: '补气',
    f_describe: `${Util.f.name}对用户${row.f_user_name}进行掉气补气操作`,
    f_state: '未知',
    f_operator: Util.f.name,
    f_filiale: Util.f.f_fengongsi,
    f_outlets: Util.f.f_parentname
  }
  return co(Vue.resetpost('rs/entity/t_fillgas', data, {resolveMsg: null, rejectMsg: '掉气补气存库失败'}))
}

let getParamGen = function * (id, row) {
  let data = {
    f_card_id: id + '',
    f_alias: row.f_alias
  }
  return co(Vue.resetpost('rs/logic/stairCard', {data: data}, {resolveMsg: null, rejectMsg: '获取写卡参数失败'}))
}

let OffGasAddGasService = {
  install (Vue, options) {
    // 给vue增添对话框显示方法
    Vue.OffGasAddGasService = Vue.prototype.$OffGasAddGasService = OffGasAddGasService
  },
  offGasAddGas (model, row) {
    let cardGen = offGasAddGasGen(model, row)
    return co(cardGen)
  }
}

export default OffGasAddGasService
